package com.bgs.travelapp.scenic.service.impl;

import com.bgs.travelapp.order.entity.ScenicOrderEntity;
import com.bgs.travelapp.order.mapper.ScenicOrdersMapper;
import com.bgs.travelapp.page.MyPageHelper;
import com.bgs.travelapp.page.PageRequest;
import com.bgs.travelapp.page.PageResult;
import com.bgs.travelapp.scenic.entity.ScenicEntity;
import com.bgs.travelapp.scenic.mapper.ScenicMapper;
import com.bgs.travelapp.scenic.service.ScenicService;
import com.bgs.travelapp.fileManager.service.FileManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ScenicServiceImpl implements ScenicService {

    @Resource
    ScenicMapper scenicMapper;

    @Resource
    FileManagerService fileManagerService;
    
    @Autowired
    ScenicOrdersMapper scenicOrdersMapper;

    @Resource
    ScenicService scenicService;

    /**
     * 分页全查景区景点信息（后端管理展示景点信息）
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult getScenicAll(PageRequest pageRequest) {
        Example example=new Example(ScenicEntity.class);
        example.createCriteria().andEqualTo("delFlag",0);
        MyPageHelper.startPage(pageRequest);
        List<ScenicEntity> scenicEntities =scenicMapper.selectByExample(example);
        PageResult pageEntity = MyPageHelper.pageInfo(scenicEntities);
        return pageEntity;
    }

    /**
     * 根据城市id查询
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult getScenicAllByCityId(PageRequest pageRequest, Integer cityId) {
        Example example=new Example(ScenicEntity.class);
        example.createCriteria().andEqualTo("delFlag",0).andEqualTo("cityId",cityId);
        MyPageHelper.startPage(pageRequest);
        List<ScenicEntity> scenicEntities =scenicMapper.selectByExample(example);
        PageResult pageEntity = MyPageHelper.pageInfo(scenicEntities) ;
        return pageEntity;
    }

    /**
     * 新增景区景点（后端管理）
     * @param file
     * @param scenicEntity
     * @return
     */
    @Override
    public Integer saveScenic(MultipartFile[] file, ScenicEntity scenicEntity) {
        StringBuffer imageId = new StringBuffer();
        for (MultipartFile f:file){
            try {
                int imageIds = fileManagerService.uploadPicture(f);
                if(imageIds>0){
                    if(!imageId.toString().equals("")){
                        imageId.append(",");
                    }
                    imageId.append(imageIds);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        scenicEntity.setCreateTime(new Date());
        scenicEntity.setUpdateTime(new Date());
        scenicEntity.setImageId(imageId.toString());
        scenicMapper.insertUseGeneratedKeys(scenicEntity);
        return scenicEntity.getId();
    }

    /**
     * 根据id修改景区景点信息（后端）
     * @param scenicEntity
     * @return
     */
    @Override
    public Integer editScenicById(MultipartFile[] file,ScenicEntity scenicEntity) {
        StringBuffer imageId = new StringBuffer();
        if(file != null ){
            for (MultipartFile f:file){
                try {
                    int imageIds = fileManagerService.uploadPicture(f);
                    if(imageIds>0){
                        if(!imageId.toString().equals("")){
                            imageId.append(",");
                        }
                        imageId.append(imageIds);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if(!StringUtils.isEmpty(scenicEntity.getId())&& scenicEntity.getId()!=0){ //这是更新操作
            String imgId = scenicEntity.getImageId();
            if(imgId.equals("")){
                scenicEntity.setImageId(imageId.toString());
            }else{
                if(StringUtils.isEmpty(imageId.toString())){
                    imageId.append(imgId);
                }else{
                    imageId.append(",").append(imgId);
                }
            }
            scenicEntity.setImageId(imageId.toString());
            scenicEntity.setUpdateTime(new Date());
            scenicMapper.updateByPrimaryKeySelective(scenicEntity);
        }
        return scenicEntity.getId();
    }

    /**
     * 根据id删除景区景点信息（后端管理）
     * @param id
     * @return
     */
    @Override
    public Integer deleteScenicById(Integer id) {
        ScenicEntity scenicEntity = new ScenicEntity();
        scenicEntity.setId(id);
        scenicEntity.setDelFlag(1);
        int i = scenicMapper.updateByPrimaryKeySelective(scenicEntity);
        return i;
    }

    /**
     * 根据景区名称模糊查询（后端管理）
     * @param pageRequest
     * @param scenicName
     * @return
     */
    @Override
    public PageResult findScenicByName(PageRequest pageRequest, String scenicName) {
        Example example=new Example(ScenicEntity.class);
        Example.Criteria criteria =example.createCriteria();
        criteria.andLike("scenicName","%"+scenicName+"%").andEqualTo("delFlag",0);
        MyPageHelper.startPage(pageRequest);
        List<ScenicEntity> scenicEntities =scenicMapper.selectByExample(example);
        PageResult pageEntity = MyPageHelper.pageInfo(scenicEntities) ;
        return pageEntity;
    }

    /**
     * 根据城市id查询景区信息（前端管理）
     * @param cityId
     * @return
     */
    @Override
    public List<ScenicEntity> scenicAllByCirtId(Integer cityId) {
        List<ScenicEntity> scenicEntities = scenicMapper.scenicAllByCirtId(cityId);
        for (ScenicEntity scenicEntity : scenicEntities) {
            String imageId = scenicEntity.getImageId();
            String[] split = imageId.split(",");
            scenicEntity.setImageId(split[0]);
        }
        return scenicEntities;
    }

    /**
     * 后端新增时校验景区名称是否重复
     * @param scenicName
     * @return
     */
    @Override
    public List<ScenicEntity> scenicAllByScenicName(String scenicName) {
        ScenicEntity scenicEntity = new ScenicEntity();
        scenicEntity.setScenicName(scenicName);
        scenicEntity.setDelFlag(0);
        List<ScenicEntity> select = scenicMapper.select(scenicEntity);
        return select;
    }


    /**
     * 地图筛选根据城市获取景区信息 显示在地图上景区信息
     * (lzy 2022-7-15 15点20分)
     * @param cityId
     * @return
     */
    @Override
    public List<ScenicEntity> getScenicByCityId(Integer cityId) {
        List<ScenicEntity> scenicEntities = scenicMapper.scenicAllByCirtId(cityId);
        for (ScenicEntity scenicEntity : scenicEntities) {
            String imageId = scenicEntity.getImageId();
            String[] split = imageId.split(",");
            scenicEntity.setImageId(split[0]);
        }
        return scenicEntities;
    }

    /***
     * 根据景区id查询景区详细信息
     * @param id
     * @return
     */
    @Override
    public List<ScenicEntity> scenicDetailsAllById(Integer id) {
        List<ScenicEntity> scenicEntities = scenicMapper.scenicDetailsAllById(id);
        return scenicEntities;
    }

    /**
     * 根据id查询景区图片第一张
     * @param id
     * @return
     */
    @Override
    public List<ScenicEntity> getScenicImageFirstById(Integer id) {
        List<ScenicEntity> scenicEntities = scenicMapper.getScenicImageFirstById(id);
        for (ScenicEntity scenicEntity : scenicEntities) {
            String imageId = scenicEntity.getImageId();
            String[] split = imageId.split(",");
            scenicEntity.setImageId(split[0]);
        }
        return scenicEntities;
    }

    /**
     * 展示旅游足迹根据游玩景区（前台展示）
     * lzy 2022-8-4 20点23分
     * @param id
     * @return
     */
    @Override
    public Set<ScenicEntity> getScenicListByOrder(Integer id) {
        ScenicOrderEntity scenicOrderEntity = new ScenicOrderEntity();
        scenicOrderEntity.setUserId(id);
        List<ScenicOrderEntity> scenicOrderList = scenicOrdersMapper.select(scenicOrderEntity);
        ScenicEntity scenicEntity = new ScenicEntity();
        Set<ScenicEntity> scenicList = new HashSet<>();
        for (ScenicOrderEntity orderEntity : scenicOrderList) {
            scenicEntity.setId(orderEntity.getScenicId());
            ScenicEntity select = scenicMapper.selectOne(scenicEntity);
            scenicList.add(select);
        }
        return scenicList;
    }

    /**
     * 用户收藏景区，该景区收藏数加1 （前端---景区门票收藏）
     * gpd 2022-08-09 17点18分
     * @param scenicId
     * @return
     */
    @Override
    public Integer increaseScenicCollection(Integer scenicId) {
        ScenicEntity scenicEntity = new ScenicEntity();
        scenicEntity.setId(scenicId);
        List<ScenicEntity> list  = scenicService.getScenicCollection(scenicId);
        for(ScenicEntity scenic:list){
            if(scenic.getCollection()==null){
                scenic.setCollection(0);
            }
            scenicEntity.setCollection(scenic.getCollection()+1);
        }
        return scenicMapper.updateByPrimaryKeySelective(scenicEntity);
    }

    /**
     * 用户取消收藏景区，该景区收藏数减1 （前端---景区门票收藏）
     * gpd 2022-08-09 17点18分
     * @param scenicId
     * @return
     */
    @Override
    public Integer deleteScenicCollection(Integer scenicId) {
        ScenicEntity scenicEntity = new ScenicEntity();
        scenicEntity.setId(scenicId);
        List<ScenicEntity> list  = scenicService.getScenicCollection(scenicId);
        for(ScenicEntity scenic:list){
            scenicEntity.setCollection(scenic.getCollection()-1);
        }
        return scenicMapper.updateByPrimaryKeySelective(scenicEntity);
    }

    /**
     * 查询景区用户收藏数
     * gpd 2022-08-09 19点26分
     * @param scenicId
     * @return
     */
    @Override
    public List<ScenicEntity> getScenicCollection(Integer scenicId) {
        ScenicEntity scenicEntity = new ScenicEntity();
        scenicEntity.setId(scenicId);
        return scenicMapper.select(scenicEntity);
    }

    /**
     * 根据景区名称模糊查景区名称
     * hbx 2022年8月24日09:02:43
     * @param scenicName
     * @return
     */
    @Override
    public List<ScenicEntity> getScenicNameByScenicName(String scenicName) {
        Example example=new Example(ScenicEntity.class);
        Example.Criteria criteria =example.createCriteria();
        criteria.andLike("scenicName","%"+scenicName+"%").andEqualTo("delFlag",0);
        List<ScenicEntity> scenicEntities =scenicMapper.selectByExample(example);
        return scenicEntities;
    }


     /**
     * 查询景区票价 （后端订单管理，新增订单）
     * fys 2022-8-17 10：33
     * @param Id
     * @return
     */
    @Override
    public List<ScenicEntity> getTicketPrice(Integer Id) {
        ScenicEntity scenicEntity = new ScenicEntity();
        scenicEntity.setId(Id);
        return scenicMapper.select(scenicEntity);
    }
}
